<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin\Admins;
use Illuminate\Http\Request;

class AdminsController extends BaseController
{

    /**
     * 基础配置
     * @param Request $request
     */
    public function getList(Request $request)
    {
        $params = $_GET;
        $params['keywords'] = $request->input('keywords', "");
        $params['limit'] = $request->input('limit', $this->limit_arr[0]);
        $list = Admins::getList($params);
        $data['items'] = $list;
        $data['keywords'] = "keywords={$params['keywords']}";
        return view('Admin/Admins/get_list', $data);
    }

    /**
     * 添加配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if ($request->method() == "GET") return view('Admin/Admins/add');
        $request->validate([
            'account' => 'required',
            'password' => 'required',
            'phone' => 'required',
            'remark' => 'required',
        ]);
        $params = $_POST;

        $save = Admins::add($params);
        return response()->json($save);
    }

    /**
     * 修改配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {

        if ($request->method() == "GET") {
            $request->validate([
                'id' => 'required',
            ]);
            $params = $_GET;
            $data['info'] = Admins::getInfo($params);
            return view('Admin/Admins/edit', $data);
        }

        $request->validate([
            'id' => 'required',
            'account' => 'required',
            'phone' => 'required',
            'remark' => 'required',
        ]);
        $params = $_POST;

        $save = Admins::edit($params);
        return response()->json($save);
    }

    /**
     * 更改状态
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeStatus(Request $request)
    {
        $request->validate([
            'id' => 'required',
            'status' => 'required', // 状态1: 启用, 2: 禁用
        ]);
        $params = $_POST;
        $save = Admins::changeStatus($params);
        return response()->json($save);
    }

    /**
     * 删除配置
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $request->validate([
            'id' => 'required',
        ]);
        $params = $_POST;
        $save = Admins::del($params);
        return response()->json($save);
    }


    /**
     * 更改密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changePassword(Request $request)
    {
        $request->validate([
            'old_pwd' => 'required',
            'new_pwd' => 'required',
        ]);
        $params = $_POST;
        $params['user'] = $this->user;
        $save = Admins::changePassword($params);
        return response()->json($save);
    }
}